package com.xmsit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmsit.pojo.AdsAlarmStatLastMonth;
import com.xmsit.service.AdsAlarmStatLastMonthService;
import com.xmsit.mapper.AdsAlarmStatLastMonthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 徐
* @description 针对表【ads_alarm_stat_last_month(告警相关统计)】的数据库操作Service实现
* @createDate 2025-04-18 22:06:30
*/
@Service
public class AdsAlarmStatLastMonthServiceImpl extends ServiceImpl<AdsAlarmStatLastMonthMapper, AdsAlarmStatLastMonth>
    implements AdsAlarmStatLastMonthService{


    @Autowired
    private AdsAlarmStatLastMonthMapper adsAlarmStatLastMonthMapper;

    @Override
    public List<AdsAlarmStatLastMonth> getAdsAlarmStatLastMonth(AdsAlarmStatLastMonth adsAlarmStatLastMonth) {
        LambdaQueryWrapper<AdsAlarmStatLastMonth> queryWrapper = new LambdaQueryWrapper<>();
        if(adsAlarmStatLastMonth.getVin() != null) {
            queryWrapper.eq(AdsAlarmStatLastMonth::getVin, adsAlarmStatLastMonth.getVin());
        }
        if(adsAlarmStatLastMonth.getMon() != null) {
            queryWrapper.eq(AdsAlarmStatLastMonth::getMon, adsAlarmStatLastMonth.getMon());
        }
        List<AdsAlarmStatLastMonth> adsAlarmStatLastMonths = adsAlarmStatLastMonthMapper.selectList(queryWrapper);

        QueryWrapper<AdsAlarmStatLastMonth> queryWrapper1 = Wrappers.query();
        if(adsAlarmStatLastMonth.getVin() != null) {
            queryWrapper1.eq("vin", adsAlarmStatLastMonth.getVin());
        }
        QueryWrapper<AdsAlarmStatLastMonth> select = queryWrapper1.select("SUM(alarm_count) as alarmCount, SUM(l1_alarm_count) as l1AlarmCount, SUM(l2_alarm_count) as l2AlarmCount, SUM(l3_alarm_count) as l3AlarmCount");
        List<AdsAlarmStatLastMonth> adsAlarmStatLastMonths1 = adsAlarmStatLastMonthMapper.selectList(select);
        for (AdsAlarmStatLastMonth alarmStatLastMonth : adsAlarmStatLastMonths) {
            for (AdsAlarmStatLastMonth alarmStatLastMonth1 : adsAlarmStatLastMonths1) {
                if(alarmStatLastMonth.getVin().equals(alarmStatLastMonth1.getVin())){
                   alarmStatLastMonth.setAlarmCount(alarmStatLastMonth1.getAlarmCount());
            }
        }

        }
        return adsAlarmStatLastMonths;

    }
}




